<!DOCTYPE html>
<html>
<head>
    <title>图书管理</title>
</head>
<body>
    <h1>图书管理</h1>
    <!-- 借还/编辑表单 -->
    <form method="post">
        {% csrf_token %}
        <label>书名：</label><input type="text" name="title" value="{{ current_book.title|default:'' }}"><br>
        <label>作者：</label>
        <select name="author">
            {% for author in authors %}
                <option value="{{ author.id }}" {% if current_book and current_book.author.id == author.id %}selected{% endif %}>
                    {{ author.name }}
                </option>
            {% endfor %}
        </select><br>
        {% if current_book %}
            <button type="submit" name="update">更新图书</button>
            {% if current_book.borrower %}
                <button type="submit" name="return">归还图书</button>
            {% else %}
                <button type="submit" name="borrow">借阅图书</button>
            {% endif %}
        {% else %}
            <button type="submit" name="create">创建图书</button>
        {% endif %}
    </form>

    <!-- 图书列表 -->
    <h2>所有图书</h2>
    <ul>
        {% for book in books %}
            <li>
                {{ book.title }} - {{ book.author.name }}
                {% if book.borrower %}
                    （已借：{{ book.borrower.username }}）
                {% endif %}
                <a href="{% url 'book_manage' book.id %}">编辑</a>
            </li>
        {% empty %}
            <li>暂无图书</li>
        {% endfor %}
    </ul>

    <a href="{% url 'home' %}">返回主页</a>
</body>
</html>